<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/tag/taglibs.jsp" %>

function cancelUserRole() {
    $('#userRoleDialog').window('close');
}
// 添加用户角色
function saveUserRole() {
    var divs = $("#target div");
    var ids = [];
    for (var i = 0; i < divs.length; i++) {
        ids.push(divs[i].id);
    }
    $("#userRoleRoleId").val(ids.join(':'));

    var url = '<c:url value="/system/saveUserRoles.do" />';
    $('#userRoleForm').form('submit', {
        url:url,
        success:function (data) {
            var data = eval('(' + data + ')');
            if (data.success) {
                alert(data.message)
            }
        }
    });
}
// 用户权限查询方法
function editUserRoles(value, loginId, name) {
    $('.drag').remove();
    $('#showLoginId').html(loginId);
    $('#showUserName').html(name);
    $('#userRoleUserId').val(value);
    $.post('<c:url value="/system/queryByUserRoles.do" />',
        {'userId':value},
        function (data) {
            for (var i = 0; i < data.length; i++) {
                var tempDiv = '<div id="' + data[i].roleId + '" class="drag">' + data[i].roleName + '</div>';
                $('#target').append(tempDiv);
            }
            initDraggable();
        }, 'json');
    $.post('<c:url value="/system/queryByNotUserRoles.do" />',
        {'userId':value},
        function (data) {
            for (var i = 0; i < data.length; i++) {
                var tempDiv = '<div id="' + data[i].roleId + '" class="drag">' + data[i].roleName + '</div>';
                $('#source').append(tempDiv);
            }
            initDraggable();
        }, 'json');
    $('#userRoleDialog').window('open');
    initdroppable();
}

function initDraggable() {
    $('.drag').draggable({
        proxy:'clone',
        revert:true,
        cursor:'auto',
        onStartDrag:function () {
            $(this).draggable('options').cursor = 'not-allowed';
            $(this).draggable('proxy').addClass('dp');
        },
        onStopDrag:function () {
            $(this).draggable('options').cursor = 'auto';
        }
    });
}

function initdroppable() {
    $('#source').droppable({
        //accept:'#d1,#d3',
        onDragEnter:function (e, source) {
            $(source).draggable('options').cursor = 'auto';
            $(source).draggable('proxy').css('border', '1px solid red');
            $(this).addClass('over');
        },
        onDragLeave:function (e, source) {
            $(source).draggable('options').cursor = 'not-allowed';
            $(source).draggable('proxy').css('border', '1px solid #ccc');
            $(this).removeClass('over');
        },
        onDrop:function (e, source) {
            $(this).append(source)
            $(this).removeClass('over');
        }
    });
    $('#target').droppable({
        //accept:'#d1,#d3',
        onDragEnter:function (e, source) {
            $(source).draggable('options').cursor = 'auto';
            $(source).draggable('proxy').css('border', '1px solid red');
            $(this).addClass('over');
        },
        onDragLeave:function (e, source) {
            $(source).draggable('options').cursor = 'not-allowed';
            $(source).draggable('proxy').css('border', '1px solid #ccc');
            $(this).removeClass('over');
        },
        onDrop:function (e, source) {
            $(this).append(source)
            $(this).removeClass('over');
        }
    });
}

<!-- 下面的方法已经不再使用，为第一版TreeGrid方式的角色管理 -->
// 用户角色权限初始化方法
function initDataGridTree() {
    $('#roleFunctionTable').treegrid({
        fit:true, //表格自适应
        fitColumns:true, //列自适应
        nowrap:true, //是否换行
        autoRowHeight:false, //自动行高
        striped:true,
        collapsible:true, //是否可折叠
        remoteSort:false,
        rownumbers:true, //行号
        singleSelect:true, //是否单选
        scrollbarSize:0,
        loadMsg:"数据加载中.....",
        animate:false,
        idField:'id',
        treeField:'id',
        frozenColumns:[
            [
                {title:'角色编号', field:'id', width:200,
                    formatter:function (value) {
                        return '<span style="color:red">' + value + '</span>';
                    }
                }
            ]
        ],
        columns:[
            [
                {title:'角色名称', field:'text', width:150},
            ]
        ],
        toolbar:"#treeGridToolbar",
        onBeforeLoad:function (row, param) {
            if (row) {
                $(this).treegrid('options').url = '<c:url value="/system/queryRoleFunctions.do?roleId=" />' + row.id;
            } else {
                $(this).treegrid('options').url = '<c:url value="/system/queryUserRoles.do" />';
            }
        }
    });
}

// 用户权限查询方法
function searchUserRoles(value) {
    $('#userRoleDialog').window('open');
    loadData(value);
}

function loadData(value) {
    $.post('<c:url value="/system/queryUserRoles.do" />',
        {'userId':value},
        function (data) {
            $('#roleFunctionTable').treegrid('loadData', data);
        }, 'json');
}
// 删除用户角色
function deleteUserRole() {
    var node = $('#roleFunctionTable').treegrid('getSelected');
    alert(node.id);
    if (node) {
        $('#roleFunctionTable').treegrid('remove', node.id);
    }
}
